﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using YunSpace.Data;
using YunSpace.Data.Extensions;
using YunSpace.Domain.Entity.WorkFlow;
using YunSpace.Domain.IRepository.WorkFlow;

namespace YunSpace.Repository.WorkFlow
{
    public class DBConnectionRepository: IDBConnectionRepository
    {
        private MySqlDbHelper dbHelper = new MySqlDbHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public DBConnectionRepository()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">DBConnectionEntity实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(DBConnectionEntity model)
        {
            string sql = @"INSERT INTO WorkFlow_DBConnection
				(F_Id,F_Name,F_Type,F_ConnectionString,F_Note) 
				VALUES(@ID,@Name,@Type,@ConnectionString,@Note)";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = model.F_Id },
                new MySqlParameter("@Name", MySqlDbType.VarChar, 500){ Value = model.F_Name },
                new MySqlParameter("@Type", MySqlDbType.VarChar, 500){ Value = model.F_Type },
                new MySqlParameter("@ConnectionString", MySqlDbType.VarChar, 2000){ Value = model.F_ConnectionString },
                model.F_Note == null ? new MySqlParameter("@Note", MySqlDbType.VarChar, 2000) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.VarChar, 2000) { Value = model.F_Note }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">DBConnectionEntity实体类</param>
        public int Update(DBConnectionEntity model)
        {
            string sql = @"UPDATE WorkFlow_DBConnection SET 
				F_Name=@Name,F_Type=@Type,F_ConnectionString=@ConnectionString,F_Note=@Note
				WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@Name", MySqlDbType.VarChar, 500){ Value = model.F_Name },
                new MySqlParameter("@Type", MySqlDbType.VarChar, 500){ Value = model.F_Type },
                new MySqlParameter("@ConnectionString", MySqlDbType.VarChar, 2000){ Value = model.F_ConnectionString },
                model.F_Note == null ? new MySqlParameter("@Note", MySqlDbType.VarChar, 2000) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.VarChar, 2000) { Value = model.F_Note },
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = model.F_Id }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM WorkFlow_DBConnection WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
            };
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<DBConnectionEntity> DataReaderToList(MySqlDataReader dataReader)
        {
            List<DBConnectionEntity> List = new List<DBConnectionEntity>();
            DBConnectionEntity model = null;
            while (dataReader.Read())
            {
                model = new DBConnectionEntity();
                model.F_Id = dataReader.GetString(0);
                model.F_Name = dataReader.GetString(1);
                model.F_Type = dataReader.GetString(2);
                model.F_ConnectionString = dataReader.GetString(3);
                if (!dataReader.IsDBNull(4))
                    model.F_Note = dataReader.GetString(4);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<DBConnectionEntity> GetAll()
        {
            string sql = "SELECT * FROM WorkFlow_DBConnection";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<DBConnectionEntity> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM DBConnection";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public DBConnectionEntity Get(Guid id)
        {
            string sql = "SELECT * FROM WorkFlow_DBConnection WHERE F_Id=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
                new MySqlParameter("@ID", MySqlDbType.VarChar, 50){ Value = id }
            };
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<DBConnectionEntity> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }
    }
}